﻿using ArtMath.Core.Data;
using System;
using System.Collections.Generic;

namespace ArtMath.PointCollection
{
    [Serializable]
    public class CustomCollection : PointCollectionBase
    {
        List<Predicate<PointD>> rules;
        Func<PointD, PointD?> getNearestPoint;
        public CustomCollection(Func<PointD, PointD?> getNearestPoint, params Predicate<PointD>[] rules)
        {
            this.rules = new List<Predicate<PointD>>(rules);
            this.getNearestPoint = getNearestPoint;
        }
        public override bool Contains(PointD pt)
        {
            if (!AfterContains(pt) || rules == null) return false;
            foreach (Predicate<PointD> item in rules)
            {
                if (item == null) continue;
                if (item(pt))
                {
                    return true;
                }
            }
            return false;
        }

        public override PointD? GetNearestPoint(PointD pt)
        {
            if (rules != null)
            {
                foreach (Predicate<PointD> item in rules)
                {
                    if (item == null) continue;
                    if (item(pt))
                    {
                        return pt;
                    }
                }
            }
            return getNearestPoint?.Invoke(pt);
        }

        protected override PointCollectionBase Intersect(PointCollectionBase collection)
        {
            return CustomIntersect(collection);
        }
    }
}
